from idlelib import browser

import requests
from lxml import etree
import pymysql


#爬取数据 size-页数
def theme_date(size):
    for num in range(size):
        url = 'https://www.ligongku.com/search?tab=all&groupCon=0&costCon=allOeuvre&sortCon=mixedPriorities&q=毕业设计&pageSize=12&pageNum='+str(num)
        header = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
        }
        res_html = requests.get(url=url,headers=header)
        etree_html = etree.HTML(res_html.text)
        content = etree_html.xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/article/div')
        for _ in content:
            try:
                type = str(_.xpath('./div/div[1]/span[1]/span/a/h4/text()')).replace("·圈子\\xa0\\xa0\\xa0","").replace("['","").replace("']","")
                detailUrl = "https://www.ligongku.com"+str(_.xpath('./div/div[2]/div/a')[0].get('href'))
                detailText = ''.join(_.xpath('./div/div[2]/div/p/text()'))
                res_html2 = requests.get(url = detailUrl,headers=header)
                etree_html2 = etree.HTML(res_html2.text)
                name = ''.join(etree_html2.xpath('//*[@class="_0f2e0"]/p/text()')).replace("：","").replace(" ","").replace("[","").replace("【","").replace("]","").replace("】","").replace("毕业设计","",1).replace("\n","").replace(" ","")
                imgUrl = ''.join(etree_html2.xpath('//*[@class="photoListClickLink _9023e"]/img[1]/@src'))
                data1 = dataObject(type,name,detailUrl,detailText,imgUrl)
                data1.to_string()
                if name != "":
                    save_sql(data1)
            except:
                print("第"+str(num+1)+"页数据！")

def img_detail():
    url="https://www.ligongku.com/resource/3100"
    header = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
    }
    html = requests.get(url=url,headers=header)
    print(html.text)
    # etree_html = etree.HTML(html.text)
    # # img_src = etree_html.xpath('//*[@id="__layout"]/div/div[2]/div[2]/div/div[1]/div[2]/img[1]/@src')
    # detailText = etree_html.xpath('//*[@id="3100"]/*')
    # print(detailText)

class dataObject():
    def __init__(self, type, name, detailUrl, detailText, imgUrl):
        self.type = type
        self.name = name
        self.detailUrl = detailUrl
        self.detailText = detailText
        self.imgUrl = imgUrl
    def to_string(self):
        print("type:"+self.type+";name:"+self.name+";detailUrl:"+self.detailUrl+";detailText:"+self.detailText+";imgUrl:"+self.imgUrl)
#保存数据
def save_sql(dataObject):
    count = pymysql.connect(
        host='47.94.156.155',  # 数据库地址
        port=3306,  # 数据库端口
        user='gd_plan',  # 数据库账号
        password='yl2vXBS%FjGoSfby',  # 数据库密码
        db='gd_plan'  # 数据库名
    )
    # 创建数据库对象
    db = count.cursor()
    # 写入sql
    print("类型："+dataObject.type)
    sql = f"insert into history_design_theme(type,name,detail_url,detail_text,img_url) values ('{dataObject.type}','{dataObject.name}','{dataObject.detailUrl}'," \
          f"'{dataObject.detailText}','{dataObject.imgUrl}')"
    # 执行sql
    db.execute(sql)
    # 保存修改内容
    count.commit()
    db.close()


if __name__ == "__main__":
    # d = dataObject("oo1","002","003","004","oo5")
    # d.to_string()
    # save_sql(d)
    theme_date(25)
    # img_detail()